'use strict';
var app = angular.module('sbAdminApp')
	.controller('AddcourseCtrl', ['$scope', '$rootScope', '$state', 'CourseService',
		function($scope, $rootScope, $state, CourseService) {
			$scope.addcourse = function() {
				console.log(angular.toJson($scope.course));
				CourseService.save(
					$scope.course,
					function(result) {
						console.log(result);
						$scope.showmsg = true;
						$scope.alertmsg = result.message;
						if (result.success) {

						} else {

						}
					});
			};
		}
	]);

app.controller('EditcourseCtrl', ['$scope', '$rootScope', '$state', '$stateParams', 'CourseService', 'utils',
	function($scope, $rootScope, $state, $stateParams, CourseService, utils) {
		// console.log($rootScope.courselist);
		$scope.$watch(function() {
			return $rootScope.courselist;
		}, function() {
			if ($rootScope.courselist != undefined) {
				$scope.course = utils.findCourseFromList($rootScope.courselist, $stateParams.couId);
			}
		}, true);

		// console.log($rootScope.courselist);
		$scope.editcourse = function() {
			console.log(angular.toJson($scope.course));
			CourseService.save(
				$scope.course,
				function(result) {
					console.log(result);
					$scope.showmsg = true;
					$scope.alertmsg = result.message;
					if (result.success) {

					} else {

					}
				});
		};
	}
]);

app.controller('CourseCtrl', ['$scope', '$rootScope', '$state', '$stateParams', '$cookieStore', 'CourseService', 'utils',
	function($scope, $rootScope, $state, $stateParams, $cookieStore, CourseService, utils) {

		$scope.$watch(function() {
			return $rootScope.courselist;
		}, function() {
			if ($rootScope.courselist != undefined) {
				$scope.course = utils.findCourseFromList($rootScope.courselist, $stateParams.couId);
			}
		}, true);
		// $state.go('dashboard.course.info', {
		// 	couId: $stateParams.couId
		// });
	}
]);

app.controller('CourseInfoCtrl', function($scope, $rootScope, $state, $stateParams, $cookieStore, CourseService, utils) {

	$scope.$watch(function() {
		return $rootScope.courselist;
	}, function() {
		if ($rootScope.courselist != undefined) {
			$scope.course = utils.findCourseFromList($rootScope.courselist, $stateParams.couId);
		}
	}, true);
});

app.controller('CourseStudentsCtrl', function($scope, $rootScope, $state, $stateParams, $cookieStore, CourseService, utils, StudentCourseService) {

	StudentCourseService.getCourseStudents({
		couId: $stateParams.couId
	}, function(students) {
		console.log(students);
		$scope.students = students;
	});
});

app.controller('CourseLocateCtrl', function($scope, $rootScope, $state, $stateParams, $cookieStore, CourseService, utils, StudentCourseService, LocateService) {

	LocateService.getLocateByCouId({
		couId: $stateParams.couId
	}, function(locates) {
		console.log(locates);
		$scope.locates = locates;
	});

	$scope.$watch(function() {
		return $rootScope.courselist;
	}, function() {
		if ($rootScope.courselist != undefined) {
			$scope.course = utils.findCourseFromList($rootScope.courselist, $stateParams.couId);
			$scope.checkModel = $scope.course.locState;
		}
	}, true);

	$scope.changeLocState = function() {
		LocateService.setLocateState({
			couId: $stateParams.couId
		}, $.param({
			locState: $scope.course.locState
		}), function(data) {
			if (data.success) {
				$state.transitionTo($state.current, $stateParams, {
					reload: true,
					inherit: false,
					notify: true
				});
			}
		});
	};
});

app.controller('CoursePublicCtrl', function($scope, $rootScope, $state, $stateParams, $cookieStore, CourseService, utils, NoticeInfoService) {
	// 获得通知
	NoticeInfoService.getAll({
		couId: $stateParams.couId
	}, function(notices) {
		// console.log(notice);
		if (notices[0] != undefined) {
			$scope.notices = notices;
		} else {
			$scope.notices = undefined;
		}
		console.log(notices);
	});

	$scope.deleteNotice = function(notId) {
		NoticeInfoService.delete({
			notId: notId
		}, {}, function(data) {
			if (data.success) {
				$state.transitionTo($state.current, $stateParams, {
					reload: true,
					inherit: false,
					notify: true
				});
			}
		})
	};
});

app.controller('CoursePublicAddCtrl', function($scope, $rootScope, $state, $stateParams, $cookieStore, CourseService, utils, NoticeInfoService) {

	$scope.addnotice = function () {
		$scope.notice.couId = $stateParams.couId;
		NoticeInfoService.save({}, $scope.notice, function (data) {
			$scope.alertmsg = data.message;
			if(data.success){
				$state.go('dashboard.course.public', $stateParams);
			}
		});
	};
});